<template>
    <div>
        <section>
            <HomeSwiper />
        </section>
        <!-- 服务流程 -->
        <section>
            <Customize ref="customizeRef" />
        </section>

        <!-- 数据统计 -->
        <section>
            <WhyChoose />
        </section>
        <!-- 核心优势 -->
        <section>
            <Theme />
        </section>

        <section>
            <Team ref="teamRef" />
        </section>
    </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted, h, nextTick } from 'vue'

import {
    Swiper as HomeSwiper,
    Customize,
    WhyChoose,
    Theme,
    Team
} from '@/components/home'

/**
 * 服务流程
 */
const customizeRef = ref(null)
const teamRef = ref(null)
const isScrolled = ref(false)
const handleScroll = () => {
    isScrolled.value = window.scrollY > 50
}

onMounted(() => {
    window.addEventListener('scroll', handleScroll)

    const observer = new IntersectionObserver(
        entries => {
            entries.forEach(entry => {
                if (entry.isIntersecting) {
                    entry.target.classList.add('animate-in')
                }
            })
        },
        {
            threshold: 0.1,
            rootMargin: '0px 0px -100px 0px'
        }
    )

    // 等待 DOM 更新后获取子组件中的元素
    nextTick(() => {
        if (customizeRef.value) {
            // 获取子组件中的动画元素
            const customizeElements = customizeRef.value.getAnimateElements()
            customizeElements.forEach(el => {
                observer.observe(el)
            })
        }

        // 监听其他组件的动画元素
        document.querySelectorAll('.animate-trigger').forEach(el => {
            observer.observe(el)
        })
    })

    // 获取团队组件中需要动画的元素
    if (teamRef.value) {
        const teamElements = teamRef.value.getAnimateElements()
        teamElements.forEach(el => {
            observer.observe(el)
        })
    }
})
// onUnmounted(() => {
//     window.removeEventListener('scroll', handleScroll)
//     if (observer) {
//         observer.disconnect()
//     }
// })
</script>
<style scoped>
.main-container {
    height: 100vh;
}

.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 50;
    transition: all 0.3s;
}

.header-scrolled {
    background-color: white;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.nav {
    width: 1300px;
    margin: 0 auto;
    padding: 0 16px;
    height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.logo-wrapper {
    display: flex;
    align-items: center;
}

.logo-wrapper img {
    height: 32px;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 32px;
}

.nav-link {
    color: #666;
}

.nav-link:hover {
    color: #52c41a;
}

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: #555;
}

.animate-trigger {
    opacity: 0;
    transform: translateY(30px);
    transition: all 0.8s ease-out;
}

.animate-in {
    opacity: 1;
    transform: translateY(0);
}

.animate-trigger:nth-child(2) {
    transition-delay: 0.2s;
}
.animate-trigger:nth-child(3) {
    transition-delay: 0.3s;
}
.animate-trigger:nth-child(4) {
    transition-delay: 0.4s;
}
.animate-trigger:nth-child(5) {
    transition-delay: 0.5s;
}

:deep(.swiper) {
    overflow: visible;
}

:deep(.swiper-slide) {
    opacity: 0.4;
    transition: opacity 0.3s ease;
}

:deep(.swiper-slide-active),
:deep(.swiper-slide-next),
:deep(.swiper-slide-next + .swiper-slide),
:deep(.swiper-slide-next + .swiper-slide + .swiper-slide) {
    opacity: 1;
}
</style>
